package com.wxdc.nmgyt.nmgytwydc.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.wxdc.nmgyt.nmgytwydc.activity.MainActivity;
import com.wxdc.nmgyt.nmgytwydc.app.MyApp;

import java.util.List;

/**
 * Created by Administrator on 2018/5/4.
 */

public class LocationServiceUtil {
    private Location mLocation;
    private LocationManager mLocationManager;
    private Context mContext;
    private final String TAG = this.getClass().getSimpleName();
    private static LocationServiceUtil instance;
    private LocationServiceUtil(Context context) {
        this.mContext = context;
        init();
    }

    public static LocationServiceUtil getInstance(Context context){
        if (instance == null) {
            instance = new LocationServiceUtil(context);
        }
        return instance;
    }

    public Location getLocation() {
        
        return mLocation;
    }

    private void init() {
        mLocationManager = MyApp.getInstance().mLocationManager;
        List<String> providers = mLocationManager.getProviders(true);
        for (String provider : providers) {
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            }
            mLocation = mLocationManager.getLastKnownLocation(provider);
            mLocationManager.requestLocationUpdates(provider, 2 * 1000, 0,mLocationListener );
        }
    }
    public LocationListener mLocationListener = new LocationListener() {
        //位置改变时调用
        @Override
        public void onLocationChanged(Location location) {
            mLocation = location;
            Log.i(TAG, "onLocationChanged: ----------" + mLocation.getProvider() + "-------" + mLocation.getLongitude());
        }

        //状态改变时调用
        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
            Log.i(TAG, "onStatusChanged: ---------" + s);
        }

        //provider生效时调用
        @Override
        public void onProviderEnabled(String s) {
            Log.i(TAG, "onProviderEnabled: ----------" + s);
        }

        //provider失效时调用
        @Override
        public void onProviderDisabled(String s) {
            Log.i(TAG, "onProviderDisabled: --------------" + s);
        }
    };

}
